package com.study.manage.service;

import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.study.manage.dal.BlogWhitelistRepository;
import com.study.manage.dal.DiscountRepository;
import com.study.manage.dal.EvaluateApplyRepository;
import com.study.manage.dal.EvaluateNewRepository;
import com.study.manage.dal.NewsRepository;
import com.study.manage.domain.BlogWhitelist;
import com.study.manage.domain.Discount;
import com.study.manage.domain.EvaluateApply;
import com.study.manage.domain.EvaluateNew;
import com.study.manage.domain.News;
import com.study.manage.domain.enumm.BlogWhitelistType;
import com.study.manage.domain.enumm.DiscountType;
import com.study.manage.domain.enumm.LiveDieState;
import com.study.manage.utils.JsonMapper;


@Component("discountService")
public class DiscountService {

    private static final Logger LOGGER = LoggerFactory.getLogger(DiscountService.class);

    @Autowired
    private DiscountRepository discountRepository;
    @Autowired
    private BlogWhitelistRepository blogWhitelistRepository;
    @Autowired
    private EvaluateApplyRepository evaluateApplyRepository;
    @Autowired
    private EvaluateNewRepository evaluateNewRepository;
    @Autowired
    private NewsRepository newsRepository;

    private JsonMapper jsonMapper = new JsonMapper();

    public void discountUser(DiscountType type, Long extId, Long userId, Long evaluateApplyId) {
        try {
            List<Discount> discounts =
                    discountRepository.findByTypeAndExtIdAndState(type, extId, LiveDieState.LIVE);
            BlogWhitelistType bwt = null;
            if (type == DiscountType.Evaluate || type == DiscountType.H5Apply) {
                bwt = BlogWhitelistType.Dis;
            } else {
                bwt = BlogWhitelistType.SupDis;
            }
            if (!discounts.isEmpty()) {
                Discount discount = discounts.get(0);
                List<BlogWhitelist> bwls =
                        blogWhitelistRepository.findByBlogIdAndUserIdAndTypeAndState(
                                discount.blogId, userId, bwt, LiveDieState.LIVE);
                if (bwls.isEmpty()) {
                    BlogWhitelist whitelist = new BlogWhitelist();
                    whitelist.blogId = discount.blogId;
                    whitelist.isSys = true;
                    whitelist.type = bwt;
                    whitelist.userId = userId;
                    blogWhitelistRepository.save(whitelist);
                }
            }

            if (type == DiscountType.Evaluate) {
                EvaluateApply ea = evaluateApplyRepository.findOne(evaluateApplyId);
                if (ea.advName != null && !"放弃领取".equals(ea.advName)) {
                    List<EvaluateNew> ens = evaluateNewRepository
                            .findByEvaluateIdAndState(ea.evaluateId, LiveDieState.LIVE);
                    for (EvaluateNew en : ens) {
                        if (en.newsId == null) {
                            continue;
                        }
                        News news = newsRepository.findOne(en.newsId);
                        List<BlogWhitelist> bwls = blogWhitelistRepository
                                .findByBlogIdAndUserIdAndTypeAndState(news.blogId, userId,
                                        BlogWhitelistType.SupDis, LiveDieState.LIVE);
                        if (bwls.isEmpty()) {
                            BlogWhitelist whitelist = new BlogWhitelist();
                            whitelist.blogId = news.blogId;
                            whitelist.isSys = true;
                            whitelist.type = BlogWhitelistType.SupDis;
                            whitelist.userId = userId;
                            blogWhitelistRepository.save(whitelist);
                        }
                    }
                }

                // Map<String, Object> map = jsonMapper.fromJson(ea.text, Map.class);
                // String key = "语文朗读大会定制课程";
                // if (map.containsKey(key)) {
                // String value = map.get(key).toString();
                // if ("免费领取价值598元的三好网专属定制语文朗读大会在线体验课程套装（含“朗读进阶大师公开课”及“三好网中高级名师中小学一对一直播课”）"
                // .equals(value)) {
                // discountUser(DiscountType.EvaluatePrice, extId, userId);
                // List<BlogWhitelist> bwls =
                // blogWhitelistRepository.findByBlogIdAndUserIdAndTypeAndState(43746l,
                // userId, BlogWhitelistType.SupDis, LiveDieState.LIVE);
                // if (bwls.isEmpty()) {
                // BlogWhitelist whitelist = new BlogWhitelist();
                // whitelist.blogId = 43746l;
                // whitelist.isSys = true;
                // whitelist.type = BlogWhitelistType.SupDis;
                // whitelist.userId = userId;
                // blogWhitelistRepository.save(whitelist);
                // }
                //
                // List<BlogWhitelist> bwls2 =
                // blogWhitelistRepository.findByBlogIdAndUserIdAndTypeAndState(43750l,
                // userId, BlogWhitelistType.SupDis, LiveDieState.LIVE);
                // if (bwls2.isEmpty()) {
                // BlogWhitelist whitelist = new BlogWhitelist();
                // whitelist.blogId = 43750l;
                // whitelist.isSys = true;
                // whitelist.type = BlogWhitelistType.SupDis;
                // whitelist.userId = userId;
                // blogWhitelistRepository.save(whitelist);
                // }
                //
                // }
                // }
            }
        } catch (Exception e) {
            LOGGER.error("折扣出错", e);
        }
    }

}
